package cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestflowlog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestflowlog.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestflowlog.DeviceLatestFlowLogDO;
import cn.iocoder.yudao.module.mqtt.service.devicelatestflowlog.DeviceLatestFlowLogService;

@Tag(name = "管理后台 - 水量日志")
@RestController
@RequestMapping("/mqtt/device-latest-flow-log")
@Validated
public class DeviceLatestFlowLogController {

    @Resource
    private DeviceLatestFlowLogService deviceLatestFlowLogService;

    @PostMapping("/create")
    @Operation(summary = "创建水量日志")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:create')")
    public CommonResult<Long> createDeviceLatestFlowLog(@Valid @RequestBody DeviceLatestFlowLogSaveReqVO createReqVO) {
        return success(deviceLatestFlowLogService.createDeviceLatestFlowLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水量日志")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:update')")
    public CommonResult<Boolean> updateDeviceLatestFlowLog(@Valid @RequestBody DeviceLatestFlowLogSaveReqVO updateReqVO) {
        deviceLatestFlowLogService.updateDeviceLatestFlowLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水量日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:delete')")
    public CommonResult<Boolean> deleteDeviceLatestFlowLog(@RequestParam("id") Long id) {
        deviceLatestFlowLogService.deleteDeviceLatestFlowLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除水量日志")
                @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:delete')")
    public CommonResult<Boolean> deleteDeviceLatestFlowLogList(@RequestParam("ids") List<Long> ids) {
        deviceLatestFlowLogService.deleteDeviceLatestFlowLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水量日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:query')")
    public CommonResult<DeviceLatestFlowLogRespVO> getDeviceLatestFlowLog(@RequestParam("id") Long id) {
        DeviceLatestFlowLogDO deviceLatestFlowLog = deviceLatestFlowLogService.getDeviceLatestFlowLog(id);
        return success(BeanUtils.toBean(deviceLatestFlowLog, DeviceLatestFlowLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水量日志分页")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:query')")
    public CommonResult<PageResult<DeviceLatestFlowLogRespVO>> getDeviceLatestFlowLogPage(@Valid DeviceLatestFlowLogPageReqVO pageReqVO) {
        PageResult<DeviceLatestFlowLogDO> pageResult = deviceLatestFlowLogService.getDeviceLatestFlowLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceLatestFlowLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水量日志 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest-flow-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceLatestFlowLogExcel(@Valid DeviceLatestFlowLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceLatestFlowLogDO> list = deviceLatestFlowLogService.getDeviceLatestFlowLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "水量日志.xls", "数据", DeviceLatestFlowLogRespVO.class,
                        BeanUtils.toBean(list, DeviceLatestFlowLogRespVO.class));
    }

}